package com.service;

import com.alibaba.fastjson.JSON;
import com.entity.JiaZheng_xtl;
import com.utils.EntityFactory;
import com.utils.ExecUtil;
import com.utils.Result;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@WebServlet(urlPatterns = {"/jiazheng_add","/jiazheng_search","/jiazheng_edit"})
public class JiaZhengService_xtl extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        if (!req.getServletPath().equals("/jiazheng_search")) {
            Result.fail(404);
            return;
        }
        List<JiaZheng_xtl> list = new ArrayList<>();
        List<Object> params = new ArrayList<>();
        String sql = "select * from jiazheng_xtl ";
        boolean judge = true;
        if (!req.getParameter("phone").isEmpty()) {
            params.add(req.getParameter("phone"));
            if (judge) {
                judge = false;
                sql = sql + "where";
            } else {
                sql = sql + "and";
            }
            sql = sql + "phone like \"%\"?\"%\"";
        } else if (!req.getParameter("username").isEmpty()) {
            params.add(req.getParameter("username"));
            if (judge) {
                judge = false;
                sql = sql + "where";
            } else {
                sql = sql + "and";
            }
            sql = sql + "username like \"%\"?\"%\"";
        } else if (!req.getParameter("time1").isEmpty()) {
            params.add(req.getParameter("time1"));
            if (judge) {
                judge = false;
                sql = sql + "where";
            } else {
                sql = sql + "and";
            }
            sql = sql + "time >=?";
        } else if (!req.getParameter("time2").isEmpty()) {
            params.add(req.getParameter("time2"));
            if (judge) {
                judge = false;
                sql = sql + "where";
            } else {
                sql = sql + "and";
            }
            sql = sql + "time <=?";
        }
        try {
            ResultSet rt = (ResultSet) ExecUtil.execute(sql, params, req, false);
            while (rt.next()) {
                EntityFactory entityFactory = new EntityFactory<>(JiaZheng_xtl.class);
                JiaZheng_xtl o = (JiaZheng_xtl) entityFactory.startCombine(rt);
                list.add(o);
            }
            resp.getWriter().write(JSON.toJSONString(list));
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            try {
                ExecUtil.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String sql = null;
        List<Object> params = null;
        String data = null;

        if (req.getServletPath().equals("/jiazheng_add")) {
            params = new ArrayList<>();
            sql = "insert into jiazheng_xtl (username,phone,time) values (?,?,?)";
                params.add(req.getParameter("username"));
                params.add(req.getParameter("phone"));
                params.add(req.getParameter("time"));
                data = "添加成功";
            } else if (req.getServletPath().equals("/jiazheng_edit")) {
                params = new ArrayList<>();
                sql = "update jiazheng_xtl set username=?,phone=? where id=?";
                params.add(req.getParameter("username"));
                params.add(req.getParameter("phone"));
                params.add(req.getParameter("id"));
                data = "编辑成功";
            } else {
                resp.getWriter().write("404");
            }
            try {
                ExecUtil.execute(sql, params, req, true);
                resp.getWriter().write(data);
            } catch (Exception e) {
                throw new RuntimeException(e);
            } finally {
                try {
                    ExecUtil.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }

